import { login } from '@/api/index'
import { getToken, setToken, removeToken } from '@/utils/auth'


const user = {
  state: {
    token: getToken(),
  },

  mutations: {
    SET_TOKEN: (state, token) => {
      state.token = token
    },
  },

  actions: {
    // 登录
    Login({ commit }, userInfo) {
      const rememberMe = userInfo.rememberMe
      return new Promise((resolve, reject) => {
        login(userInfo).then(res => {
          if(res.code == 0){
            setToken(res.data, rememberMe);
            commit('SET_TOKEN', res.data)
          }
          resolve(res)
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}

export const logOut = (commit) => {
  commit('SET_TOKEN', '')
  removeToken()
}

export default user
